// src/main/java/com/neusoft/SPNursingHome/dao/BedRecordDao.java
package com.neusoft.SPNursingHome.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.SPNursingHome.po.BedRecord;
import com.neusoft.SPNursingHome.po.BedRecordWithClient;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface BedRecordDao extends BaseMapper<BedRecord> {
    /**
     * 三表联查：bed_record + client + bed
     * 支持按 clientName 模糊、bedBeginTime 区间、bedRecordStatus 精确过滤
     * 如果所有筛选都为空，则返回全部。
     */
    @Select({
            "<script>",
            "SELECT",
            "  br.bed_record_id    AS bedRecordId,",
            "  br.client_id        AS clientId,",
            "  br.bed_id           AS bedId,",
            "  br.bed_begin_time   AS bedBeginTime,",
            "  br.bed_end_time     AS bedEndTime,",
            "  br.bed_record_status AS bedRecordStatus,",
            "  c.client_name       AS clientName,",
            "  b.room_id           AS roomId,",
            "  b.floor_id          AS floorId,",
            "  b.bed_status        AS bedStatus,",
            "  b.bed_code          AS bedCode",
            "FROM bed_record br",
            "  LEFT JOIN client c ON br.client_id = c.client_id",
            "  LEFT JOIN bed b    ON br.bed_id    = b.bed_id",
            "<where>",
            "  <if test='clientName != null and clientName.trim() != \"\"'>",
            "    AND c.client_name LIKE CONCAT('%', #{clientName}, '%')",
            "  </if>",
            "  <if test='beginTime != null'>",
            "    AND br.bed_begin_time &gt;= #{beginTime}",
            "  </if>",
            "  <if test='endTime != null'>",
            "    AND br.bed_begin_time &lt;= #{endTime}",
            "  </if>",
            "  <if test='bedRecordStatus != null'>",
            "    AND br.bed_record_status = #{bedRecordStatus}",
            "  </if>",
            "</where>",
            "ORDER BY br.bed_begin_time DESC",
            "</script>"
    })
    IPage<BedRecordWithClient> selectWithClientByCondition(
            IPage<BedRecordWithClient> page,
            @Param("clientName") String clientName,
            @Param("beginTime") java.util.Date beginTime,
            @Param("endTime")   java.util.Date endTime,
            @Param("bedRecordStatus") Integer bedRecordStatus
    );
}
